package org.ls.petclinic.web;

import org.ls.petclinic.dao.PetsDao;
import org.ls.petclinic.dao.VetSpecialtiesDao;
import org.ls.petclinic.dao.VetsDao;
import org.ls.petclinic.dao.impl.PetsDaoImpl;
import org.ls.petclinic.dao.impl.VetSpecialtiesDaoImpl;
import org.ls.petclinic.dao.impl.VetsDaoImpl;
import org.ls.petclinic.untils.JdbcUtils;
import org.ls.petclinic.untils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 浪矢先生
 * @description
 * @create 2022-12-05 23:47
 */
@WebServlet(urlPatterns = "/addVets")
public class AddVets extends BaseServlet{

  private VetsDao vetsDao=new VetsDaoImpl();
  private VetSpecialtiesDao vetSpecialtiesDao=new VetSpecialtiesDaoImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        //请求参数
        String name = req.getParameter("name");
        String[] specialtyIds = req.getParameterValues("specialtyId");
        //添加,返回添加完的主键id
        Long vetId=(Long)vetsDao.add(name);
        int result=0;
        for (String specialtyId : specialtyIds) {
            long l = Long.parseLong(specialtyId);
            result+= vetSpecialtiesDao.add(l,vetId);
        }
        JdbcUtils.commitAndClose();
        if (result>0){
            req.setAttribute("msg", "添加兽医成功!");
        }else {
            req.setAttribute("msg", "添加兽医失败!");
        }
        //页面转发
        req.getRequestDispatcher("/jsp/getVet.jsp").forward(req, resp);
//        resp.sendRedirect(req.getContextPath() + "/jsp/getUnHealthyPet.jsp");
    }
}
